import java.util.Scanner;

public class MailClient {

  private static String user;
  private static String password;
  private static String smtpHost;
  private static String popHost;
  private static int smtpPort;
  private static int popPort;
  private static Scanner scanner;

  private static void readEmails() {
    int number = -1;
    Pop3Service popService = new Pop3Service(user, password, popHost, popPort);
    System.out.println("Podaj numer wiadomosci");
    number = Integer.parseInt(scanner.nextLine());
    popService.readEmails(number);
  }

  private static void sendEmail() {
    String reciepientEmail;
    String message;
    SmtpService smtpService = new SmtpService(user, password, smtpHost, smtpPort);
    System.out.println("Podaj adresata");
    reciepientEmail = scanner.nextLine();
    System.out.println("Podaj tresc wiadomosci");
    message = scanner.nextLine();
    System.out.println(reciepientEmail + ", " + message + ";");
    smtpService.sendMail(reciepientEmail, message);
  }

  private static void checkEmails() {
    int number = -1;
    Pop3Service popService = new Pop3Service(user, password, popHost, popPort);
    number = popService.checkNumberOfEmails();
    System.out.println("Masz " + number + " wiadomosci");

  }

  public static void main(String[] args) {

    if (args.length != 6) {
      System.out.println("Zla ilosc argumentow wejsciowych");
      return;
    }

    user = args[0];
    password = args[1];
    smtpHost = args[2];
    popHost = args[3];
    try {
      smtpPort = Integer.parseInt(args[4]);
      popPort = Integer.parseInt(args[5]);
    } catch (NumberFormatException e) {
      System.out.println("Zly format argumentow popPort lub smtpPort");
    }

    String inputString = "";
    scanner = new Scanner(System.in);

    int inputValue = 10;

    while (inputValue != 0) {
      System.out.println();
      System.out.println("============================");
      System.out.println();
      System.out.println("Wybierz opcje: \n");
      System.out.println("1) Wyslij wiadomosc");
      System.out.println("2) Odczytaj wiadomosc");
      System.out.println("3) Sprawdz ilosc wiadomosci");

      System.out.println("");
      System.out.println("0) Wyjscie");
      inputString = scanner.nextLine();

      inputValue = Integer.parseInt(inputString);

      switch (inputValue) {
        case 1:
          sendEmail();
          break;
        case 2:
          readEmails();
          break;
        case 3:
          checkEmails();
          break;
        case 0:
          break;
        default:
          System.out.println("Podales zla wartosc");
          break;
      }
    }
    scanner.close();
  }

}
